﻿/* 
 *  <copyright file="KeyboardDevice.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using SobrietyEngine.Events;

namespace SobrietyEngine.Input.Devices
{
    public class KeyboardDevice:BaseTranslator, IInputDevice
    {
        private static KeyboardState currentState;
        private static KeyboardState previousState;

        public KeyboardDevice(IEventRouter er): base(er, false, 0)
        {
        }

        void Initialize()
        {
            //base.Initialize();
            previousState = Keyboard.GetState();
        }

        public override void Update(GameTime gameTime)
        {
            currentState = Keyboard.GetState();

            //CGuid CurrentPlayerGuid;
            Keys[] pressed = currentState.GetPressedKeys();
            Keys[] oldPressed = previousState.GetPressedKeys();

            foreach (Keys k in pressed)
            {
                if (previousState.IsKeyUp(k))
                {
                    Events.Input.KeyPress kped = new Events.Input.KeyPress(this, k);
                    EventRouterGameService.RaiseEvent(this, kped);
                }
            }

            foreach (Keys k in oldPressed)
            {
                if (currentState.IsKeyUp(k))
                {
                    Events.Input.KeyRelease kped = new Events.Input.KeyRelease(this,k);
                    EventRouterGameService.RaiseEvent(this, kped);
                }
            }

            previousState = currentState;
        }  
    }
}